/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'discoveries',
                model: {
                    url: _global.url.api + 'discovery',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove'
                    },
                    edit: function (e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '修改发现',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function () {
                                _this.data.cover_layout = $("input[name='cover_layout']:checked").val();
                                _this.data.img = $("input[name='imgs']").val();
                                if (_this.data.img == '' || _this.data.img == undefined) {
                                    Alert({msg: '请传入一张图片', type: 'error'});
                                    return false;
                                }
                                _this.data.extra = $('#extra').val();
                                if (_this.data.extra == '') {
                                    Alert({msg: '请传入系列名称', type: 'error'});
                                    return false;
                                }
                                _this.data.url = $('#url').val();
                                if (_this.data.url == '') {
                                    Alert({msg: '请传入链接', type: 'error'});
                                    return false;
                                }
                                _this.data.title = $('#title').val();
                                if (_this.data.title == '') {
                                    Alert({msg: '请传入下方标题', type: 'error'});
                                    return false;
                                }
                                _this.data.url = $('#content').val();
                                if (_this.data.content == '') {
                                    Alert({msg: '请输入下方正文', type: 'error'});
                                    return false;
                                }
                                _this.data.cover_title = $('#cover_title').val();
                                if (_this.data.cover_title == '' && _this.data.cover_layout != 0) {
                                    Alert({msg: '请输入封面标题', type: 'error'});
                                    return false;
                                }
                                _this.data.start_time = $('#dt_start').val();
                                _this.data.end_time = $('#dt_end').val();
                                if (_this.data.start_time != '' || _this.data.end_time != '') {
                                    if (_this.data.start_time == '' || _this.data.end_time == '') {
                                        Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                        return false;
                                    }
                                    var date1 = new Date(Date.parse(_this.data.start_time));
                                    var date2 = new Date(Date.parse(_this.data.end_time));
                                    if (date1.getTime() > date2.getTime()) {
                                        Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                                        return false;
                                    }
                                }else{
                                    Alert({msg: '开始时间和结束时间不能为空', type: 'error'});
                                    return false;
                                }
                                _this.data.intro = $('#intro').val();
                                _this.data.id = _this.data.id;
                                if (model.put()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function (e) {
                            _this.change(e);
                        });
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.fetch();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加发现',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.cover_layout = $("input[name='cover_layout']:checked").val();
                        _this.data.img = $("input[name='imgs']").val();
                        if (_this.data.img == '' || _this.data.img == undefined) {
                            Alert({msg: '请传入一张图片', type: 'error'});
                            return false;
                        }
                        _this.data.extra = $('#extra').val();
                        if (_this.data.extra == '') {
                            Alert({msg: '请传入系列名称', type: 'error'});
                            return false;
                        }
                        _this.data.url = $('#url').val();
                        if (_this.data.url == '') {
                            Alert({msg: '请传入链接', type: 'error'});
                            return false;
                        }
                        _this.data.title = $('#title').val();
                        if (_this.data.title == '') {
                            Alert({msg: '请传入下方标题', type: 'error'});
                            return false;
                        }
                        _this.data.url = $('#content').val();
                        if (_this.data.content == '') {
                            Alert({msg: '请输入下方正文', type: 'error'});
                            return false;
                        }
                        _this.data.cover_title = $('#cover_title').val();
                        if (_this.data.cover_title == '' && _this.data.cover_layout != 0) {
                            Alert({msg: '请输入封面标题', type: 'error'});
                            return false;
                        }
                        _this.data.start_time = $('#dt_start').val();
                        _this.data.end_time = $('#dt_end').val();
                        if (_this.data.start_time != '' || _this.data.end_time != '') {
                            if (_this.data.start_time == '' || _this.data.end_time == '') {
                                Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                                return false;
                            }
                            var date1 = new Date(Date.parse(_this.data.start_time));
                            var date2 = new Date(Date.parse(_this.data.end_time));
                            if (date1.getTime() > date2.getTime()) {
                                Alert({msg: '开始时间不能大于结束时间', type: 'error'});
                                return false;
                            }
                        }else{
                            Alert({msg: '开始时间和结束时间不能为空', type: 'error'});
                            return false;
                        }
                        _this.data.intro = $('#intro').val();
                        if (new Model({
                                url: _global.url.api + 'discovery',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
                $('.long_date').on('click', function () {
                    WdatePicker({
                        el: this.id,
                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                    });
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        console.log(123123123);
                        console.log('contentWindow', $("#upload-frame")[0].contentWindow.image);
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});